package com.tzb.ssm.controller.admin;

import com.tzb.ssm.entity.admin.User;
import com.tzb.ssm.service.admin.UserService;
import com.tzb.ssm.util.CpachaUtil;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description 系统操作类控制器
 * @Author tzb
 * @Date 2021/5/8 22:40
 * @Version 1.0
 **/
@Controller
@RequestMapping("/system")
public class SystemController {

    @Autowired
    private UserService userService;

    /**
     * 系统登录后的主页
     * @param model
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index(ModelAndView model) {
        model.setViewName("system/index");
        model.addObject("name", "晴空万里");
        return model;
    }

    /**
     * 系统登录后的欢迎页
     * @param model
     * @return
     */
    @RequestMapping(value = "/welcome", method = RequestMethod.GET)
    public ModelAndView welcome(ModelAndView model) {
        model.setViewName("system/welcome");
        return model;
    }


    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView login(ModelAndView model) {
        model.setViewName("system/login");
        return model;
    }

    /**
     * 登录表单提交
     *
     * @param user
     * @param cpacha
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> loginAct(User user, String cpacha, HttpServletRequest request) {
        Map<String, String> ret = new HashMap<>();
        if (user == null) {
            ret.put("type", "error");
            ret.put("msg", "请填写用户信息");
            return ret;
        }

        if (StringUtils.isEmpty(cpacha)) {
            ret.put("type", "error");
            ret.put("msg", "请填写验证码");
            return ret;
        }

        if (StringUtils.isEmpty(user.getUsername())) {
            ret.put("type", "error");
            ret.put("msg", "请填写用户名");
            return ret;
        }

        if (StringUtils.isEmpty(user.getPassword())) {
            ret.put("type", "error");
            ret.put("msg", "请填写密码");
            return ret;
        }

        Object loginCpacha = request.getSession().getAttribute("loginCpacha");
        if (loginCpacha == null) {
            ret.put("type", "error");
            ret.put("msg", "会话超时，请刷新页面！");
            return ret;
        }

        if (!cpacha.toUpperCase().equals(loginCpacha.toString().toUpperCase())) {
            ret.put("type", "error");
            ret.put("msg", "验证码错误！");
            return ret;
        }

        User findUser = userService.findByUsername(user.getUsername());
        if (findUser == null) {
            ret.put("type", "error");
            ret.put("msg", "用户不存在！");
            return ret;
        }

        if (!user.getPassword().equals(findUser.getPassword())) {
            ret.put("type", "error");
            ret.put("msg", "密码错误");
            return ret;
        }

        //将登录的用户放在session
        request.getSession().setAttribute("admin", findUser);

        ret.put("type", "success");
        ret.put("msg", "登录成功！");
        return ret;
    }


    /**
     * 本系统所有的验证码都采用此方法
     *
     * @param vcodeLen
     * @param width
     * @param height
     * @param cpachaType 用来区别验证码的类型，传入字符串
     * @param request
     * @param response
     */
    @RequestMapping(value = "get_cpacha", method = RequestMethod.GET)
    public void generateCapcha(
            @RequestParam(name = "v1", required = false, defaultValue = "4") Integer vcodeLen,
            @RequestParam(name = "w", required = false, defaultValue = "100") Integer width,
            @RequestParam(name = "h", required = false, defaultValue = "30") Integer height,
            @RequestParam(name = "type", required = true, defaultValue = "loginCpacha") String cpachaType,
            HttpServletRequest request,
            HttpServletResponse response) {
        CpachaUtil cpachaUtil = new CpachaUtil(vcodeLen, width, height);
        final String generatorVCode = cpachaUtil.generatorVCode();
        request.getSession().setAttribute(cpachaType, generatorVCode);
        final BufferedImage image = cpachaUtil.generatorRotateVCodeImage(generatorVCode, true);
        try {
            ImageIO.write(image, "gif", response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
